clear all


use "$root\data\use.dta", replace // corrected (potentially) misaligned pre-season weather and drought



capture erase "$root/results_replication/tables/decomp_corn_sur.xls"
capture erase "$root/results_replication/tables/decomp_corn_sur.txt"t"
capture erase "$root/results_replication/tables/decomp_soybeans_sur.xls"
capture erase "$root/results_replication/tables/decomp_soybeans_sur.txt"

**# Decomposition
foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00


**## With FE models
	foreach y in pplanted plratio hratio yield production {
	qui reghdfe ln`crop'_`y' tot_b tot_p tot_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100 &`crop'_restrict==1, absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)	
		if "`y'"=="pplanted" {
			qui outreg2 using "$root/results_replication/appendix_tables/a4_decomp_`crop'_fe.xls", bdec(4) sdec(4) keep(tot_b tot_p tot_g) nocon adjr2 replace excel
		}
		else {
			qui outreg2 using "$root/results_replication/appendix_tables/a4_decomp_`crop'_fe.xls", bdec(4) sdec(4) keep(tot_b tot_p tot_g) nocon adjr2 excel
		}
	}
	restore
}


**## With SUR
foreach crop in corn soybeans {
	global fe i.fips i.state#c.year i.state#c.year2 i.year
	keep if irrigated_ratio < 0.1 & N_year_`crop' > 10 & lng > -100 & `crop'_restrict==1
	capture drop resid_*
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00

	qui {
		foreach x in tot_b tot_p tot_g pdsi_w_b pdsi_w_p pdsi_w_g {
			reghdfe `x', absorb($fe) resid
			predict r_`x', residuals
		}
		vl create tbins = (tbin*b tbin*p tbin*g)
		foreach x in $tbins {
			reghdfe `x', absorb($fe) resid
			predict r_`x', residuals
		}
	}

	* constraints
	constraint 1 [ln`crop'_pplanted]r_tot_b + [ln`crop'_plratio]r_tot_b + [ln`crop'_hratio]r_tot_b + [ln`crop'_yield]r_tot_b = [ln`crop'_production]r_tot_b
	constraint 2 [ln`crop'_pplanted]r_tot_p +[ln`crop'_plratio]r_tot_p + [ln`crop'_hratio]r_tot_p + [ln`crop'_yield]r_tot_p = [ln`crop'_production]r_tot_p
	constraint 3 [ln`crop'_hratio]r_tot_g + [ln`crop'_yield]r_tot_g = [ln`crop'_production]r_tot_g

	constraint 4 [ln`crop'_pplanted]r_pdsi_w_b + [ln`crop'_plratio]r_pdsi_w_b + [ln`crop'_hratio]r_pdsi_w_b + [ln`crop'_yield]r_pdsi_w_b = [ln`crop'_production]r_pdsi_w_b
	constraint 5 [ln`crop'_pplanted]r_pdsi_w_p +[ln`crop'_plratio]r_pdsi_w_p + [ln`crop'_hratio]r_pdsi_w_p + [ln`crop'_yield]r_pdsi_w_p = [ln`crop'_production]r_pdsi_w_p
	constraint 6 [ln`crop'_hratio]r_pdsi_w_g + [ln`crop'_yield]r_pdsi_w_g = [ln`crop'_production]r_pdsi_w_g

	constraint 11 [ln`crop'_pplanted]r_tbin2c_b + [ln`crop'_plratio]r_tbin2c_b + [ln`crop'_hratio]r_tbin2c_b + [ln`crop'_yield]r_tbin2c_b = [ln`crop'_production]r_tbin2c_b
	constraint 12 [ln`crop'_pplanted]r_tbin2c_p +[ln`crop'_plratio]r_tbin2c_p + [ln`crop'_hratio]r_tbin2c_p + [ln`crop'_yield]r_tbin2c_p = [ln`crop'_production]r_tbin2c_p
	constraint 13 [ln`crop'_hratio]r_tbin2c_g + [ln`crop'_yield]r_tbin2c_g = [ln`crop'_production]r_tbin2c_g

	constraint 21 [ln`crop'_pplanted]r_tbin5c_b + [ln`crop'_plratio]r_tbin5c_b + [ln`crop'_hratio]r_tbin5c_b + [ln`crop'_yield]r_tbin5c_b = [ln`crop'_production]r_tbin5c_b
	constraint 22 [ln`crop'_pplanted]r_tbin5c_p +[ln`crop'_plratio]r_tbin5c_p + [ln`crop'_hratio]r_tbin5c_p + [ln`crop'_yield]r_tbin5c_p = [ln`crop'_production]r_tbin5c_p
	constraint 23 [ln`crop'_hratio]r_tbin5c_g + [ln`crop'_yield]r_tbin5c_g = [ln`crop'_production]r_tbin5c_g

	constraint 31 [ln`crop'_pplanted]r_tbin8c_b + [ln`crop'_plratio]r_tbin8c_b + [ln`crop'_hratio]r_tbin8c_b + [ln`crop'_yield]r_tbin8c_b = [ln`crop'_production]r_tbin8c_b
	constraint 32 [ln`crop'_pplanted]r_tbin8c_p +[ln`crop'_plratio]r_tbin8c_p + [ln`crop'_hratio]r_tbin8c_p + [ln`crop'_yield]r_tbin8c_p = [ln`crop'_production]r_tbin8c_p
	constraint 33 [ln`crop'_hratio]r_tbin8c_g + [ln`crop'_yield]r_tbin8c_g = [ln`crop'_production]r_tbin8c_g

	constraint 41 [ln`crop'_pplanted]r_tbin11c_b + [ln`crop'_plratio]r_tbin11c_b + [ln`crop'_hratio]r_tbin11c_b + [ln`crop'_yield]r_tbin11c_b = [ln`crop'_production]r_tbin11c_b
	constraint 42 [ln`crop'_pplanted]r_tbin11c_p +[ln`crop'_plratio]r_tbin11c_p + [ln`crop'_hratio]r_tbin11c_p + [ln`crop'_yield]r_tbin11c_p = [ln`crop'_production]r_tbin11c_p
	constraint 43 [ln`crop'_hratio]r_tbin11c_g + [ln`crop'_yield]r_tbin11c_g = [ln`crop'_production]r_tbin11c_g

	constraint 51 [ln`crop'_pplanted]r_tbin14c_b + [ln`crop'_plratio]r_tbin14c_b + [ln`crop'_hratio]r_tbin14c_b + [ln`crop'_yield]r_tbin14c_b = [ln`crop'_production]r_tbin14c_b
	constraint 52 [ln`crop'_pplanted]r_tbin14c_p +[ln`crop'_plratio]r_tbin14c_p + [ln`crop'_hratio]r_tbin14c_p + [ln`crop'_yield]r_tbin14c_p = [ln`crop'_production]r_tbin14c_p
	constraint 53 [ln`crop'_hratio]r_tbin14c_g + [ln`crop'_yield]r_tbin14c_g = [ln`crop'_production]r_tbin14c_g

	constraint 61 [ln`crop'_pplanted]r_tbin17c_b + [ln`crop'_plratio]r_tbin17c_b + [ln`crop'_hratio]r_tbin17c_b + [ln`crop'_yield]r_tbin17c_b = [ln`crop'_production]r_tbin17c_b
	constraint 62 [ln`crop'_pplanted]r_tbin17c_p +[ln`crop'_plratio]r_tbin17c_p + [ln`crop'_hratio]r_tbin17c_p + [ln`crop'_yield]r_tbin17c_p = [ln`crop'_production]r_tbin17c_p
	constraint 63 [ln`crop'_hratio]r_tbin17c_g + [ln`crop'_yield]r_tbin17c_g = [ln`crop'_production]r_tbin17c_g

	constraint 71 [ln`crop'_pplanted]r_tbin20c_b + [ln`crop'_plratio]r_tbin20c_b + [ln`crop'_hratio]r_tbin20c_b + [ln`crop'_yield]r_tbin20c_b = [ln`crop'_production]r_tbin20c_b
	constraint 72 [ln`crop'_pplanted]r_tbin20c_p+[ln`crop'_plratio]r_tbin20c_p + [ln`crop'_hratio]r_tbin20c_p + [ln`crop'_yield]r_tbin20c_p = [ln`crop'_production]r_tbin20c_p
	constraint 73 [ln`crop'_hratio]r_tbin20c_g + [ln`crop'_yield]r_tbin20c_g = [ln`crop'_production]r_tbin20c_g

	constraint 81 [ln`crop'_pplanted]r_tbin23c_b + [ln`crop'_plratio]r_tbin23c_b + [ln`crop'_hratio]r_tbin23c_b + [ln`crop'_yield]r_tbin23c_b = [ln`crop'_production]r_tbin23c_b
	constraint 82 [ln`crop'_pplanted]r_tbin23c_p +[ln`crop'_plratio]r_tbin23c_p + [ln`crop'_hratio]r_tbin23c_p + [ln`crop'_yield]r_tbin23c_p = [ln`crop'_production]r_tbin23c_p
	constraint 83 [ln`crop'_hratio]r_tbin23c_g + [ln`crop'_yield]r_tbin23c_g = [ln`crop'_production]r_tbin23c_g

	constraint 91 [ln`crop'_pplanted]r_tbin26c_b + [ln`crop'_plratio]r_tbin26c_b + [ln`crop'_hratio]r_tbin26c_b + [ln`crop'_yield]r_tbin26c_b = [ln`crop'_production]r_tbin26c_b
	constraint 92 [ln`crop'_pplanted]r_tbin26c_p +[ln`crop'_plratio]r_tbin26c_p + [ln`crop'_hratio]r_tbin26c_p + [ln`crop'_yield]r_tbin26c_p = [ln`crop'_production]r_tbin26c_p
	constraint 93 [ln`crop'_hratio]r_tbin26c_g + [ln`crop'_yield]r_tbin26c_g = [ln`crop'_production]r_tbin26c_g

	constraint 101 [ln`crop'_pplanted]r_tbin29c_b + [ln`crop'_plratio]r_tbin29c_b + [ln`crop'_hratio]r_tbin29c_b + [ln`crop'_yield]r_tbin29c_b = [ln`crop'_production]r_tbin29c_b
	constraint 102 [ln`crop'_pplanted]r_tbin29c_p+[ln`crop'_plratio]r_tbin29c_p + [ln`crop'_hratio]r_tbin29c_p + [ln`crop'_yield]r_tbin29c_p = [ln`crop'_production]r_tbin29c_p
	constraint 103 [ln`crop'_hratio]r_tbin29c_g + [ln`crop'_yield]r_tbin29c_g = [ln`crop'_production]r_tbin29c_g

	constraint 111 [ln`crop'_pplanted]r_tbin32c_b + [ln`crop'_plratio]r_tbin32c_b + [ln`crop'_hratio]r_tbin32c_b + [ln`crop'_yield]r_tbin32c_b = [ln`crop'_production]r_tbin32c_b
	constraint 112 [ln`crop'_pplanted]r_tbin32c_p +[ln`crop'_plratio]r_tbin32c_p + [ln`crop'_hratio]r_tbin32c_p + [ln`crop'_yield]r_tbin32c_p = [ln`crop'_production]r_tbin32c_p
	constraint 113 [ln`crop'_hratio]r_tbin32c_g + [ln`crop'_yield]r_tbin32c_g = [ln`crop'_production]r_tbin32c_g

	constraint 121 [ln`crop'_pplanted]r_tbin35c_b + [ln`crop'_plratio]r_tbin35c_b + [ln`crop'_hratio]r_tbin35c_b + [ln`crop'_yield]r_tbin35c_b = [ln`crop'_production]r_tbin35c_b
	constraint 122 [ln`crop'_pplanted]r_tbin35c_p +[ln`crop'_plratio]r_tbin35c_p + [ln`crop'_hratio]r_tbin35c_p + [ln`crop'_yield]r_tbin35c_p = [ln`crop'_production]r_tbin35c_p
	constraint 123 [ln`crop'_hratio]r_tbin35c_g + [ln`crop'_yield]r_tbin35c_g = [ln`crop'_production]r_tbin35c_g

	constraint 131 [ln`crop'_pplanted]r_tbin38c_b + [ln`crop'_plratio]r_tbin38c_b + [ln`crop'_hratio]r_tbin38c_b + [ln`crop'_yield]r_tbin38c_b = [ln`crop'_production]r_tbin38c_b
	constraint 132 [ln`crop'_pplanted]r_tbin38c_p +[ln`crop'_plratio]r_tbin38c_p + [ln`crop'_hratio]r_tbin38c_p + [ln`crop'_yield]r_tbin38c_p = [ln`crop'_production]r_tbin38c_p
	constraint 133 [ln`crop'_hratio]r_tbin38c_g + [ln`crop'_yield]r_tbin38c_g = [ln`crop'_production]r_tbin38c_g

* SUR
	qui {
	sureg (ln`crop'_pplanted r_tot_b r_tot_p r_pdsi_w_b r_pdsi_w_p r_tbin*_b r_tbin*_p)  ///
		  (ln`crop'_plratio r_tot_b r_tot_p r_pdsi_w_b r_pdsi_w_p r_tbin*_b r_tbin*_p) ///
		  (ln`crop'_hratio r_tot_b r_tot_p r_tot_g r_pdsi_w_b r_pdsi_w_p r_pdsi_w_g r_tbin*_b r_tbin*_p r_tbin*_g) ///
		  (ln`crop'_yield r_tot_b r_tot_p r_tot_g r_pdsi_w_b r_pdsi_w_p r_pdsi_w_g r_tbin*_b r_tbin*_p r_tbin*_g) ///
		  (ln`crop'_production r_tot_b r_tot_p r_tot_g r_pdsi_w_b r_pdsi_w_p r_pdsi_w_g r_tbin*_b r_tbin*_p r_tbin*_g) ///
		  , constraints(1/3)

	est store sur_`crop'
	}

	outreg2 sur_`crop' keep(r_tot*) using "$root/results_replication/tables/decomp_`crop'_sur.xls", bdec(4) sdec(4) excel replace
		
	restore
}	

